﻿using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using Orther.Model.param;
using Orther.Model.requestparam;
using Orther.Service.Miyao;
using Project.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.ManageUser;
using VOL.Entity.DomainModels;
using static Dapper.SqlMapper;

namespace Orther.BLL.Miyao
{
    public class WeixiudanBLL
    {
        WeixiudanService weixiudanService = new WeixiudanService();
        public TData<List<qx_wxd_nr>> GetPageList(WeixiudanParam param, Pagination pagination)
        {
            TData<List<qx_wxd_nr>> obj = new TData<List<qx_wxd_nr>>();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            obj.Data = weixiudanService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }
        public TData<decimal[]> GetSumJine(WeixiudanParam param)
        {
            TData<decimal[]> obj = new TData<decimal[]>();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            obj.Data = weixiudanService.GetSumJine(param);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<qx_wxd_nr>>> GetList(WeixiudanParam param, int? topNum)
        {
            TData<List<qx_wxd_nr>> obj = new TData<List<qx_wxd_nr>>();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            obj.Data = await weixiudanService.GetList(param, topNum);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<qx_wxd_nr>> GetObject(WeixiudanParam param)
        {
            TData<qx_wxd_nr> obj = new TData<qx_wxd_nr>();
            obj.Data = await weixiudanService.GetObject(param);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteFrom2(Guid id)
        {
            TData obj = new TData();
            WeixiudanParam param = new WeixiudanParam();
            param.Id = id;
            qx_wxd_nr nrEntity = await weixiudanService.GetObject(param);
            nrEntity.IsDeleted = 1;
            operatelog oLog = new operatelog();
            oLog.Content = @"" + UserContext.Current.UserName
                + "删除了维修单信息。单号为：【" + nrEntity.danhao + "】,客户名称为：【" + nrEntity.car_kname + "】。"
                + "车牌号：【" + nrEntity.car_carno + "】，"
                + "车型：【" + nrEntity.car_chexing + "】，"
                + "总金额：【" + nrEntity.zsum + "】。";
            oLog.Category = "删除";
            oLog.MenuModel = "维修单管理";

            oLog.CreateDate = DateTime.Now;
            oLog.CreateID = UserContext.Current.UserId;
            oLog.Creator = UserContext.Current.UserName;
            weixiudanService.UpdateForm(nrEntity, oLog);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> SaveForm(WeixiudanParams param)
        {
            qx_wxd_nr nrEntity = param.nr;
            List<qx_mx_peijian> list_peijian = param.list_peijian;
            operatelog oLog = new operatelog();
            TData obj = new TData();
            if (nrEntity.carId == null)
            {
                obj.Message = "操作失败，车牌号不能为空！";
                return obj;
            }
            if (nrEntity.nrtype == null)
            {
                obj.Message = "操作失败，单据分类为必选项！";
                return obj;
            }

            if (nrEntity.Id == Guid.Parse("00000000-0000-0000-0000-000000000000"))
            {
                nrEntity.riqi = DateTime.Now;
                nrEntity.danhao = await this.GetDanhao();
                nrEntity.CreateID = UserContext.Current.UserId;
                nrEntity.Creator = UserContext.Current.UserName;
                nrEntity.CreateDate = DateTime.Now;

                oLog.Content = UserContext.Current.UserName + "新增了维修单信息。单号为：【" + nrEntity.danhao + "】,客户名称为：【" + nrEntity.car_kname + "】";
                oLog.Category = "新增";
                oLog.MenuModel = "维修单管理";
            }
            else
            {
                nrEntity.ModifyDate = DateTime.Now;
                nrEntity.ModifyID = UserContext.Current.UserId;
                nrEntity.Modifier = UserContext.Current.UserName;
                oLog.Content = UserContext.Current.UserName + "更新了维修单信息。单号为：【" + nrEntity.danhao + "】,客户名称为：【" + nrEntity.car_kname + "】";
                oLog.Category = "修改";
                oLog.MenuModel = "维修单管理";
            }
            oLog.CreateDate = DateTime.Now;
            oLog.CreateID = UserContext.Current.UserId;
            oLog.Creator = UserContext.Current.UserName;


            weixiudanService.SaveForm(param, oLog);
            obj.Tag = 1;
            return obj;
        }

        private async Task<string> GetDanhao()
        {
            WeixiudanParam param = new WeixiudanParam();
            if (!UserContext.Current.IsSuperAdmin)
            {
                param.deptIds = UserContext.Current.GetAllChildrenDeptIds();
            }
            int num = await weixiudanService.GetDayMax(param);
            string danhao = DateTime.Now.ToString("yyyyMMdd");
            if (num < 10)
            {
                danhao = danhao + "00" + num;
            }
            else if (num >= 10 && num < 99)
            {
                danhao = danhao + "0" + num;
            }
            else
            {
                danhao = danhao + num;
            }
            return danhao;
        }


        public async Task<TData> AuditForm(Guid id)
        {
            TData obj = new TData();
            operatelog oLog = new operatelog();
            WeixiudanParam weixiudanParam = new WeixiudanParam();
            weixiudanParam.Id = id;
            qx_wxd_nr nrEntity = await weixiudanService.GetObject(weixiudanParam);
            if (nrEntity.isjiesuan == 1)
            {
                nrEntity.isjiesuan = 0;
                oLog.Content = @"" + UserContext.Current.UserName
                + "进行了撤销结算操作，将单据状态变更为【未结算状态】";
                oLog.Category = "反审核";
                obj.Message = "操作成功，该单据已被反审核！";
            }
            else
            {
                nrEntity.isjiesuan = 1;
                oLog.Content = @"" + UserContext.Current.UserName
                + "进行了结算审核操作，将单据状态变更为【结算状态】";
                oLog.Category = "审核";
                obj.Message = "操作成功，该单据已被审核！";
            }
            oLog.MenuModel = "维修单管理";

            oLog.CreateDate = DateTime.Now;
            oLog.CreateID = UserContext.Current.UserId;
            oLog.Creator = UserContext.Current.UserName;
            weixiudanService.UpdateForm(nrEntity, oLog);

            obj.Tag = 1;
            return obj;
        }

    }
}
